
<script language="Javascript">

function checkChequeNoEntered(pay_mode, cheque_no){
	if(document.getElementsByName(pay_mode)[0].value == "Cheque"){
		if(document.getElementsByName(cheque_no)[0].value.length == 0){
			alert("Please enter the cheque no");
			return false;
		}
	}
	
	return true;
}

function checkNumeric(input, display_name, optional){
	
	if(!optional){
		if(document.getElementsByName(input)[0].value.length == 0){
			alert("The " + display_name + " cannot be blank");
			return false;
		}
	}
	
	if(isNaN(document.getElementsByName(input)[0].value)){
		alert("The " + display_name + " must be numeric");
		return false;
	}
	
	return true;
}

function checkValidDate(input, display_name, optional){

	if(display_name.length == 0){
		display_name = input;
	}
	if(optional){
		if(document.getElementsByName(input)[0].value.length == 0){
			if(isDate(document.getElementsByName(input)[0].value) == false){
				alert("The " + display_name + " entered is not valid");
				return false;
			}
		}
	}else{
		if(document.getElementsByName(input)[0].value.length == 0){
			alert("The " + display_name + " cannot be blank");
			return false;
		}
		if(isDate(document.getElementsByName(input)[0].value) == false){
			alert("The " + display_name + " entered is not valid");
			return false;
		}
	}
	return true;
}

function checkNotEmpty(input, display_name){
	if(display_name.length == 0){
		display_name = input;
	}
	if(document.getElementsByName(input)[0].value.length == 0){
		alert(display_name + " cannot be blank");
		return false;
	}
	
	return true;
}

function isTime(timeString){
	if(timeString.length != 5){
		return false;
	}
	
	var patt1 = /\d\d:\d\d/;
	var result = timeString.search(patt1);
	if(result != 0){
		return false;
	}
	
	var hh = timeString.substr(0, 2);
	var nn = timeString.substr(3, 2);
	
	if(hh < 0 || hh > 23){
		return false;
	}
	
	if(nn < 0 || nn > 59){
		return false;
	}
	
	return true;
}

function isDate(dateString){
	//check the length
	if(dateString.length != 10){
		return false;
	}
	//check the format of digits to be ####-##-##
	var patt1 = /\d\d\d\d-\d\d-\d\d/;
	var result = dateString.search(patt1);
	if(result != 0){
		return false;
	}
	
	//create a date object
	var yyyy = dateString.substr(0, 4);	
	var mm = dateString.substr(5, 2) - 1;
	var dd = dateString.substr(8, 2);
	var d = new Date();
	d.setFullYear(yyyy);
	d.setMonth(mm);
	d.setDate(dd);
	
	//Check that the date is not outside it's boundaries
	var daysAvail = getDaysInMonth(yyyy, mm);
	if(dd <= 0 || dd > daysAvail){
		return false;
	}
	
	//Check that the month is not outside it's boundaries
	if(mm < 0 || mm > 12){
		return false;
	}
	
	return true;
		
}

function getDaysInMonth(yyyy, mm){
	var d2 = new Date();
	d2.setFullYear(yyyy);
	d2.setMonth(mm);

	var counter = 28;
	d2.setDate(counter);
	do{
		counter = d2.getDate();
		d2.setDate(counter + 1);
	}while(d2.getDate() > 1)
	
	return counter;
}

</script>
